package edu.jptest.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.jptest.bean.User;
import edu.jptest.serviceImp.UserService;
import edu.jptest.util.MD5Encoding;

/**
 * @author ZhangLei
 * 
 */
public class LoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 459183860917144628L;
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// 用户登录验证
	public String loginValidate() {
		UserService userService = new UserService();
		user.setUserPassword(MD5Encoding.getMD5ofStr(user.getUserPassword()));
		int userId = userService.loginValidate(user);
		if (userId != 0) {
			user = userService.getUserByUserId(userId);
			Map session = ActionContext.getContext().getSession();
			session.put("user", user);
			return SUCCESS;
		} else {
			this.addActionError("用户名或密码错误！");
			return ERROR;
		}
	}
}
